package client.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import client.Client;
import client.TcpClient;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;

import util.ClientProtocol;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class LoginGUI extends JFrame {

	private JLabel errorLbl;
	private JPanel contentPane;
	private JTextField loginField;
	private JTextField portField;
	private JTextField hostField;
	private JLabel lblPort;
	private JLabel lblPort_1;
	private JLabel hostErrorLbl;	
	ClientGUI clientFrame;

	TcpClient tc;

	/**
	 * Create the frame.
	 */
	public LoginGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 198);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] {150, 150};
		gbl_contentPane.rowHeights = new int[] {0, 0, 0, 0, 30, 30};
		gbl_contentPane.columnWeights = new double[]{0.0, 1.0};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		contentPane.setLayout(gbl_contentPane);
		
		hostErrorLbl = new JLabel("Host or Port incorrect");
		hostErrorLbl.setForeground(Color.RED);
		GridBagConstraints gbc_hostErrorLbl = new GridBagConstraints();
		gbc_hostErrorLbl.insets = new Insets(0, 0, 5, 0);
		gbc_hostErrorLbl.gridx = 1;
		gbc_hostErrorLbl.gridy = 0;
		contentPane.add(hostErrorLbl, gbc_hostErrorLbl);
		hostErrorLbl.setVisible(false);
		
		lblPort = new JLabel("Host");
		GridBagConstraints gbc_lblPort = new GridBagConstraints();
		gbc_lblPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblPort.anchor = GridBagConstraints.WEST;
		gbc_lblPort.gridx = 0;
		gbc_lblPort.gridy = 1;
		contentPane.add(lblPort, gbc_lblPort);
		
		hostField = new JTextField("localhost");
		GridBagConstraints gbc_hostField = new GridBagConstraints();
		gbc_hostField.insets = new Insets(0, 0, 5, 0);
		gbc_hostField.fill = GridBagConstraints.HORIZONTAL;
		gbc_hostField.gridx = 1;
		gbc_hostField.gridy = 1;
		contentPane.add(hostField, gbc_hostField);
		hostField.setColumns(10);
		
		lblPort_1 = new JLabel("Port");
		GridBagConstraints gbc_lblPort_1 = new GridBagConstraints();
		gbc_lblPort_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblPort_1.anchor = GridBagConstraints.WEST;
		gbc_lblPort_1.gridx = 0;
		gbc_lblPort_1.gridy = 2;
		contentPane.add(lblPort_1, gbc_lblPort_1);
		
		portField = new JTextField("1");
		GridBagConstraints gbc_portField = new GridBagConstraints();
		gbc_portField.insets = new Insets(0, 0, 5, 0);
		gbc_portField.fill = GridBagConstraints.HORIZONTAL;
		gbc_portField.gridx = 1;
		gbc_portField.gridy = 2;
		contentPane.add(portField, gbc_portField);
		portField.setColumns(10);
		
		errorLbl = new JLabel("Username incorrect");
		errorLbl.setForeground(Color.RED);
		GridBagConstraints gbc_errorLbl = new GridBagConstraints();
		gbc_errorLbl.insets = new Insets(0, 0, 5, 0);
		gbc_errorLbl.gridx = 1;
		gbc_errorLbl.gridy = 3;
		contentPane.add(errorLbl, gbc_errorLbl);
		errorLbl.setVisible(false);
		
		JLabel loginLbl = new JLabel("Username:");
		GridBagConstraints gbc_loginLbl = new GridBagConstraints();
		gbc_loginLbl.fill = GridBagConstraints.BOTH;
		gbc_loginLbl.insets = new Insets(0, 0, 5, 5);
		gbc_loginLbl.gridx = 0;
		gbc_loginLbl.gridy = 4;
		contentPane.add(loginLbl, gbc_loginLbl);
		
		loginField = new JTextField();
		GridBagConstraints gbc_loginField = new GridBagConstraints();
		gbc_loginField.anchor = GridBagConstraints.NORTH;
		gbc_loginField.fill = GridBagConstraints.HORIZONTAL;
		gbc_loginField.insets = new Insets(0, 0, 5, 0);
		gbc_loginField.gridx = 1;
		gbc_loginField.gridy = 4;
		contentPane.add(loginField, gbc_loginField);
		loginField.setColumns(10);
		
		JButton loginBtn = new JButton("It's me!");
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!portField.getText().matches(ClientProtocol.INT_MATCHER) || Integer.parseInt(portField.getText()) > 65535) {
					hostErrorLbl.setText("Port incorrect");
					hostErrorLbl.setVisible(true);
					return;
				} else {
					Client client = new Client(loginField.getText());
					tc = new TcpClient(client);
					tc.connect(hostField.getText(),Integer.parseInt(portField.getText()));
				}
				
				String loginMessage = tc.receiveMessage();
				
				if(loginMessage == null) {
					hostErrorLbl.setText("Host incorrect");
					hostErrorLbl.setVisible(true);
					return;
				} else if(loginMessage.matches(ClientProtocol.LOGIN_DUPLICATE_ERROR_MATCHER)) {
					errorLbl.setText("Name Already Exists");
					errorLbl.setVisible(true);
					return;
				} else if(loginMessage.matches(ClientProtocol.LOGIN_INVALID_ERROR_MATCHER)) {
					errorLbl.setText("Invalid Login Name");
					errorLbl.setVisible(true);
					return;
				} else if(loginMessage.matches(ClientProtocol.LOGIN_OK_MATCHER)) {
					EventQueue.invokeLater(new Runnable() {
						public void run() {
							try {
								clientFrame = new ClientGUI(tc);
								clientFrame.setVisible(true);
								clientFrame.setAlwaysOnTop(true);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					tc.start();
					setVisible(false);
				} else {
					errorLbl.setText("something WRONG");
					errorLbl.setVisible(true);
				}
			}
		});
		GridBagConstraints gbc_loginBtn = new GridBagConstraints();
		gbc_loginBtn.gridwidth = 2;
		gbc_loginBtn.fill = GridBagConstraints.BOTH;
		gbc_loginBtn.gridx = 0;
		gbc_loginBtn.gridy = 5;
		contentPane.add(loginBtn, gbc_loginBtn);
		pack();
	}

}
